using RootMotion.Dynamics;
using UnityEngine;
namespace Yoozoo.Gameplay.Liberty.AI
{
    public class NpcPuppetMaster : MonoBehaviour
    {
        private PuppetMaster _puppetMaster;
        private AIEntity _aiEntity;
        private Animator _animator;
        
        private void Awake()
        {
            _puppetMaster = transform.parent.Find("PuppetMaster").GetComponent<PuppetMaster>();
            _animator = transform.GetComponent<Animator>();
        }
        
        public void SetCurAiEntity(AIEntity aiEntity)
        {
            _aiEntity = aiEntity;
        }

        public void OnRegainBalance()
        {
            Debug.Log("OnRegainBalance");
            _puppetMaster.mode = PuppetMaster.Mode.Disabled;
            _aiEntity.seeker.transform.position = transform.position;
            _aiEntity.seeker.LocomotionAgent.SyncTransform.position = transform.position;
            transform.localPosition = Vector3.zero;
            if (_aiEntity.data.npcStyle == NpcStyle.PathWalker)
            {
                _aiEntity.graph.enabled = true;
                _aiEntity.seeker._IsEnabled = true;
                _aiEntity.seeker.LocomotionAgent.enabled = true;
            }
        }

        public void OnGetUp()
        {
            Debug.Log("OnGetUp");
        }

        public void OnLoseBalance()
        {
            Debug.Log("OnLoseBalance");
            if (_aiEntity != null && _aiEntity.data.npcStyle == NpcStyle.PathWalker)
            {
                _aiEntity.graph.enabled = false;
                _aiEntity.seeker._IsEnabled = false;
                _animator.ResetTrigger("walk");
                _aiEntity.seeker.LocomotionAgent.enabled = false;
            }
        }
        public void OnLoseBalanceFromPuppet()
        {
            Debug.Log("OnLoseBalanceFromPuppet");
        }
        public void OnLoseBalanceFromGetUp()
        {
            Debug.Log("OnLoseBalanceFromGetUp");
        }
        
        public void OnPuppetCollisionExit()
        {
            Debug.Log("OnPuppetCollisionExit");
            _puppetMaster.mode = PuppetMaster.Mode.Disabled;
        }
    }
}